import {Token}                  from '../utils/Storage';
import {DateFormat}             from '../utils/Format';
import {CALL_API, VOUCHER}      from "../constants";

/*
* 我的券
* */

const trans = fn => r => Object.assign(r, {data: {total: parseInt(r.data.count), list: _.map(r.data.list, fn)}});

const ACCUM = {
    range: ['使用时段：周一 周二 周三 周四 周五 周六 周日', '使用范围：门店消费'],
    principle: '积分原则：每消费现金1元，赠送1积分；',
    rules: [
        '1.积分计算以有效购物金额为准， 若会员在支付时采用优惠券，将只针对有效购物金额 （商品金额–优惠券金额）部分进行积分累计；',
        '2.会员消费金额中，不满1元部分不进行前后累计积分；',
        '3.会员消费积分在交易成功当天记入积分（即累计积分）；',
        '4.如发生退换货，将从积分中扣除由于退换货部分产生的积分差额；',
        '5.激活会员，立享95折。',
    ],
};

export const transVoucher =
    ({
         id,
         dw,
         shl,
         lshj,
         kssj,
         sxrq,
         rkrq,
         spid,
         sptm,
         jfyz,
         jfnr,
         xfsj,
         dhshl,
         spmch,
         shpgg,
         jfsysd,
         shpchd,
         shllast,
         shengccj,
         shlcount,
     }) => ({
        id: id.toString(),
        qr: sptm.trim(),
        vId: spid,
        name: spmch,
        time: `${kssj}${dw !== '赠送' ? '兑换' : dw},有效期至${sxrq}`,
        count: parseInt(shl),
        price: Math.abs(lshj),
        expire: sxrq,
        accumRule: _.isEmpty(jfnr) ? ACCUM.rules : jfnr.split('#'),
        accumRange: _.isEmpty(jfsysd) ? ACCUM.range : jfsysd.split('#'),
        accumPrinciple: _.isEmpty(jfyz) ? ACCUM.principle : jfyz,
        limitPrice: shpchd === 'null' ? '' : shpchd,
        accumulate: parseInt(shengccj),
        limitConsume: shpgg,
        exchangeCount: parseInt(dhshl),
        storeTime: DateFormat.format(rkrq),
        storeTotal: shlcount,
        storeExCount: shllast,
    });

//未使用
const fetchNew = ({index, size}) => ({
    [CALL_API]: {
        url: `VMembers/selectVMvouchersByMId?page=${index}&rows=${size}&m_id=${Token.getUserId()}`,
        types: VOUCHER.new.types(),
        extra: trans(transVoucher)
    }
});

//已使用
const fetchUsed = ({index, size}) => ({
    [CALL_API]: {
        url: `VMembers/selectVMconsumelogByMId?page=${index}&rows=${size}&m_id=${Token.getUserId()}`,
        types: VOUCHER.used.types(),
        extra: trans(
            ({
                 shl,
                 lshj,
                 xfsj,
                 spmch,
                 shpchd,
             }) => (
                {
                    name: spmch,
                    time: xfsj,
                    count: shl,
                    price: Math.abs(lshj),
                    limitPrice: shpchd,
                })
        )
    }
});

//已过期
const fetchExpire = ({index, size}) => ({
    [CALL_API]: {
        url: `VMembers/selectVMvouchersOverdueByMId?page=${index}&rows=${size}&m_id=${Token.getUserId()}`,
        types: VOUCHER.expire.types(),
        extra: trans(
            ({
                 shl,
                 lshj,
                 sxrq,
                 spmch,
             }) => (
                {
                    name: spmch,
                    time: sxrq,
                    count: shl,
                    price: Math.abs(lshj),
                })
        )
    }
});

//使用中
const fetchCount = ({vId, expire}) => ({
    [CALL_API]: {
        url: `VMembers/selectVVouchersShl?spid=${vId}&sxrq=${expire}&m_id=${Token.getUserId()}`,
        types: VOUCHER.count.types(),
    }
});

export const loadNew = page              => dispatch => dispatch(fetchNew(page));
export const loadUsed = page             => dispatch => dispatch(fetchUsed(page));
export const loadCount = entity          => dispatch => dispatch(fetchCount(entity));
export const loadExpire = page           => dispatch => dispatch(fetchExpire(page));
